use once_cell::sync::Lazy;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;

// 定义配置结构体
#[derive(Debug, Deserialize, Clone)]
pub struct RequestConfig {
    pub method: String,
    pub protocol_version: String,
    pub address: String,
    pub duration_seconds: u64,
    pub threads: usize,
    pub headers: Option<HashMap<String, String>>,
    pub body: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct AppConfig {
    pub requests_config: Vec<RequestConfig>,
    pub is_monitor: bool,
    pub is_recorder: bool,
    pub is_log: bool,
}

// 从 JSON 文件中加载配置
pub fn load_config() -> Result<AppConfig, serde_json::Error> {
    let mut file = File::open("./configs.json").expect("Unable to open file");
    let mut contents = String::new();
    file.read_to_string(&mut contents).expect("Unable to read file");

    serde_json::from_str(&contents)
}

// 使用 once_cell 创建全局静态配置
pub static CONFIG: Lazy<AppConfig> = Lazy::new(|| {
    load_config().expect("Failed to load configuration")
});